package sunsun.xiaoli.jiarebang.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.itboye.pondteam.db.DBHelper;
import sunsun.xiaoli.jiarebang.beans.BluetoothSearchBean;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/4/27.
 */

public class BLuetoothDeviceDBManager {
    BluetoothDeviceDBHelper mDbHelper;
    Context mContext;
    SQLiteDatabase db;

    public BLuetoothDeviceDBManager(Context context) {
        mContext = context;
        mDbHelper = new BluetoothDeviceDBHelper(mContext);
    }


//    ------------------------------------------设备密码相关---------------------------------------------------------

    public long insertBleDevice(String bleName, String bleAddr, String bleType) {
        db = mDbHelper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("ble_name", bleName);
        cv.put("ble_addr", bleAddr);
        cv.put("ble_type", bleType);
        cv.put("add_time", System.currentTimeMillis());
        long num = db.insert(BluetoothDeviceDBHelper.TABLENAME, null, cv);
        db.close();
        return num;
    }

    public long updateBleDevice(String bleName, String bleAddr) {
        db = mDbHelper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("ble_name", bleName);
        long num = db.update(BluetoothDeviceDBHelper.TABLENAME, cv, "ble_addr=?", new String[]{bleAddr});
        db.close();
        return num;
    }

    public long deleteBleDeviceByAddr(String bleAddr) {
        db = mDbHelper.getWritableDatabase();
        long num = db.delete(BluetoothDeviceDBHelper.TABLENAME, "ble_addr=?", new String[]{bleAddr});
        return num;
    }

    public BluetoothSearchBean queryBleDeviceByAddr(String bleAddr) {
        db = mDbHelper.getReadableDatabase();
        BluetoothSearchBean bluetoothSearchBean = null;
        Cursor cursor = null;
        if (this.db != null) {
            cursor = db.query(BluetoothDeviceDBHelper.TABLENAME, null, "ble_addr=?", new String[]{bleAddr}, null, null, null);
            if (cursor != null) {
                if (cursor.moveToNext()) {
                    bluetoothSearchBean = new BluetoothSearchBean();
                    bluetoothSearchBean.setId(cursor.getInt(0));
                    bluetoothSearchBean.setDeviceName(cursor.getString(1));
                    bluetoothSearchBean.setAddress(cursor.getString(2));
                    bluetoothSearchBean.setPing(cursor.getString(3));
                }
            }
        }
        return bluetoothSearchBean;
    }

    public ArrayList<BluetoothSearchBean> queryBleDevice() {
        db = mDbHelper.getReadableDatabase();
        ArrayList<BluetoothSearchBean> beanArrayList = new ArrayList();
        Cursor cursor = null;
        if (this.db != null) {
            cursor = db.query(BluetoothDeviceDBHelper.TABLENAME, null, null, null, null, null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    BluetoothSearchBean bluetoothSearchBean = new BluetoothSearchBean();
                    bluetoothSearchBean.setId(cursor.getInt(0));
                    bluetoothSearchBean.setDeviceName(cursor.getString(1));
                    bluetoothSearchBean.setAddress(cursor.getString(2));
                    bluetoothSearchBean.setPing(cursor.getString(3));
                    beanArrayList.add(bluetoothSearchBean);
                }
            }
        }
        return beanArrayList;
    }

}
